﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibHuman;


namespace Human
{
    
    public partial class frm_dmchucvudang : Form
    {
        DataSet ds_cvdang = new DataSet();
        DataSet ds = new DataSet();
        AccessData mDB = new AccessData();
        Language lan = new Language();
        int flag = 0;
        public frm_dmchucvudang()
        {
            InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
        }

        private void txt_ma_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_ten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{f4}");
        }

        private void txt_hs_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_stt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        private void enable(bool ena)
        {
            cbo_nhom.Enabled = ena;
            txt_ten.Enabled = ena;
            txt_stt.Enabled = ena;
            txt_hs.Enabled = ena;
            btnluu.Enabled = ena;
        }
        private string get_maxid()
        {
            int m_tam;
            ds = mDB.get_data("SELECT Max(to_number(ma,999)) FROM " + mDB.user + ".dmchucvudang");
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                m_tam = 1;
            }
            else
            {
                m_tam = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                m_tam += 1;
            }
            return m_tam.ToString().PadLeft(3, '0').ToString();
        }
        private void btnluu_Click(object sender, EventArgs e)
        {
            string ma = "", ten = "", nhom = "" ;
            decimal stt = 0,hs=0,m_id;
            if (txt_stt.Text != "")
            {
                stt = decimal.Parse(txt_stt.Text);
            }
            else
            {
                stt = 0;
            }
            if (txt_hs.Text != "")
            {
                hs = decimal.Parse(txt_hs.Text);
            }
            else
            {
                hs = 0;
            }
          
            ten = txt_ten.Text.Trim();
            if (flag == 0)
            {
                m_id = decimal.Parse(get_maxid());
                txt_ma.Text = m_id.ToString().PadLeft(3, '0');
            }
            else
            {
                if (txt_ma.Text != "")
                    m_id = decimal.Parse(txt_ma.Text.ToString());
            }
            ma = txt_ma.Text;
            if (cbo_nhom.SelectedIndex != -1)
                nhom = cbo_nhom.SelectedValue.ToString().Trim();
            if (txt_ten.Text == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập tên!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txt_ten.Focus();
                return;
            }
            if (!mDB.upd_dmchucvudang(ma, ten,nhom, stt,hs, mDB.user))
            {
                MessageBox.Show(lan.Change_language_MessageText("Không cập nhật được thông tin!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                btn_boqua.Focus();
                return;
            }
            flag = 0;
            load_dmdang();
            enable(false);
            btnluu.Enabled = false;
            btnthem.Focus();
            btnsua.Enabled = false;
            btn_boqua.Enabled = false;
            btnthem.Enabled = true;
        }
        private void load_dmdang()
        {
            string sql = "";
            sql = "select ma,ten,stt,hs_pc ";
            sql += " ,case nhom ";
            sql += " when 'D' then 'Đảng'";
            sql += " when 'C' then 'Công đoàn'";
            sql += " when 'T' then 'Đoàn thể'";
            sql += " else 'Khác' end as nhom";
            sql += " from " + mDB.user + ".dmchucvudang";
            sql += " order by stt";
             ds_cvdang= mDB.get_data(sql);
            dgrv_dmcvdang.DataSource= ds_cvdang.Tables[0];
        }
        private void clear_text()
        {
            //txt_ma.Text = "";
            txt_stt.Text = "0";
            txt_ten.Text = "";
            txt_hs.Text = "0";
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            enable(true);
            txt_ma.Text = get_maxid();
            clear_text();
            btnluu.Enabled = true;
            btn_boqua.Enabled = true;
            txt_ten.Focus();
            flag = 0;
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            enable(true);
            btnthem.Enabled = false;
            btnxoa.Enabled = false;
            btn_boqua.Enabled = true;
            flag = 1;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ thông tin này?"),AccessData.Msg, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                mDB.get_data("delete from " + mDB.user + ".dmchucvudang where ma='" + txt_ma.Text.Trim() + "'");
            }
            load_dmdang();
            btnthem.Enabled = true;
            btnsua.Enabled = false;
            btnxoa.Enabled = false;
            btn_boqua.Enabled = false;
          //  ena_control(true);
        }

        

        private void frm_dmchucvudang_Load(object sender, EventArgs e)
        {
            load_dmdang();
            load_cbnhom();
            btnsua.Enabled = false;
            btn_boqua.Enabled = false;
            btnxoa.Enabled = false;
            //ena_control(false);
            btnthem.Focus();
        }
        private void load_cbnhom()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("manhom",typeof(string));
            dt.Columns.Add("tennhom",typeof(string));
            DataRow nrow;
            for (int i = 0; i < 4; i++) 
            {
                nrow = dt.NewRow();
                if(i==0)
                {
                    nrow["manhom"] = "D";
                    nrow["tennhom"] = "Đảng";

                }
                else
                    if (i == 1)
                    {
                        nrow["manhom"] = "C";
                        nrow["tennhom"] = "Công đoàn";
                    }
                    else
                        if (i == 2)
                        {
                            nrow["manhom"]="T";
                            nrow["tennhom"] = "Đoàn thể";

                        }
                        else
                            if (i == 3)
                            {
                                nrow["manhom"] = "K";
                                nrow["tennhom"] = "Khác";
                            }

                dt.Rows.Add(nrow);

            }
            cbo_nhom.ValueMember = "manhom";
            cbo_nhom.DisplayMember = "tennhom";
            cbo_nhom.DataSource = dt;
            cbo_nhom.SelectedIndex = -1;
        }
        private void btn_boqua_Click(object sender, EventArgs e)
        {
            enable(false);
            btnsua.Enabled = false;
            btnxoa.Enabled = false;
            btnthem.Enabled = true;
            btn_boqua.Enabled = false;
           // ena_control(false);
            btnthem.Focus();

        }

        private void btnketthuc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_hs_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!mDB.IsNumeric(e.KeyChar.ToString()))
                e.Handled = true;
            if (e.KeyChar == 8 || e.KeyChar == '.')
                e.Handled = false;
        }

        private void txt_stt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;

        }

        private void dgrv_dmcvdang_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataRowView drv = (DataRowView)dgrv_dmcvdang.CurrentRow.DataBoundItem;
                txt_ma.Text = drv["ma"].ToString();
                txt_ten.Text = drv["ten"].ToString();
                txt_hs.Text = drv["hs_pc"].ToString();
                txt_stt.Text = drv["stt"].ToString();
                cbo_nhom.Text = drv["nhom"].ToString();
               
                 btnsua.Enabled = true;
                btnxoa.Enabled = true;
                btnsua.Focus();
                btn_boqua.Enabled = true;
                btnluu.Enabled = false;
                btnthem.Enabled = false;
            }
            catch { }
        }

        private void cbo_nhom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
      
    }
}